/*
 * Copyright 2007-2010 Jeremy Brown.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author <a href="http://jeremy-brown.com">Jeremy Brown</a>
 */

package groovy.prefuse.factory;

import java.util.Map;

import prefuse.*
import prefuse.data.*
import prefuse.data.expression.*
import prefuse.data.expression.parser.*
import prefuse.util.*
import prefuse.visual.*
import prefuse.visual.tuple.*

import groovy.util.*

public class DecoratorFactory extends AbstractFactory
{

	public DecoratorFactory()
	{
	}
	
	public DecoratorFactory(Class klass)
	{
		this.klass = klass
	}

	boolean isLeaf()
	{
		return true
	}
	
    public Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException
	{
    	def parentVisualization = builder.getCurrent()
    	if(!parentVisualization instanceof Visualization)
    	{
    		return null
    	}

		def schema = null
		if(attributes.schema)
		{
			schema = attributes.remove("schema")
		}
		else if(attributes.defaults)
		{
			schema = PrefuseLib.getVisualItemSchema()
		}

		//The defaults: attribute should have as its value a Map of key:value pairs.
		//These will be used to set default values of the schema.
		//The keys should be the UPPERCASE names of constants from VisualItem (e.g. FONT, TEXTCOLOR, INTERACTIVE)
		if(attributes.defaults)
		{
			attributes.defaults.each{key, val->
				schema.setDefault(VisualItem."$key", val)
			}
			attributes.remove("defaults")
		}

		def predicate = null
		if(attributes.predicate)
		{
			predicate = attributes.remove("predicate")
		}
		else if(attributes.filter)
		{
			predicate = attributes.remove("filter")
		}

		//Parse the String version of predicate to turn it into a Predicate
		if(predicate && !(predicate instanceof Predicate))
		{
			predicate = (Predicate)ExpressionParser.parse(predicate)
		}

		def source = attributes.remove("source")
		def group = builder.pickPrefuseGroup(attributes)
		
		def visualTable
		if(schema != null && predicate != null)
		{
			visualTable = parentVisualization.addDecorators(group, source, predicate, schema)
		}
		else if(schema != null)
		{
			visualTable = parentVisualization.addDecorators(group, source, schema)
		}
		else if(predicate != null)
		{
			visualTable = parentVisualization.addDecorators(group, source, predicate)
		}
		else
		{
			visualTable = parentVisualization.addDecorators(group, source)
		}
		
		return visualTable
	}
}
